﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EdeneraCore.Models.EdeneraCoreEntities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EdeneraCore.app.Controllers
{
    //[Route("api/[controller]/[action]")]
    //[ApiController]
    public class UserController : ControllerBase
    {
        private readonly CoreDbContext _coreDbContext;

        public UserController(CoreDbContext coreDbContext)
        {
            this._coreDbContext = coreDbContext;
        }
        [HttpGet]
        public IActionResult Login()
        {
            return Ok("hello");
        }
        [HttpGet]
        public List<User> GetList()
        {
            return _coreDbContext.User.ToList();
        }


        [HttpGet]
        public User GetUserById(int id)
        {
            return _coreDbContext.User.Single(u => u.Id == id);
        }
        [HttpPost]
        public ActionResult<string> Save([FromBody]User model)
        {
            try
            {
                if(model.Id != 0)
                {
                    var _user = _coreDbContext.User.Single(u => u.Id == model.Id);
                    _user.Name = model.Name;
                    _user.Age = model.Age;
                }
                else
                {
                    _coreDbContext.User.Add(model);
                }
                _coreDbContext.SaveChanges();
                return Ok("200");
            }
            catch (Exception ex) { return ex.Message; }
        }

    }
}